package com.jiazhong.basic.shuxin.net;

import lombok.SneakyThrows;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

// 发送方
public class DemoF1 {

    @SneakyThrows
    public static void main(String[] args) {
        // 套接字
        DatagramSocket socket = new DatagramSocket();
        // IP 接收方的IP地址
        InetAddress inetAddress = InetAddress.getByName("127.0.0.1");
        // port
        int port = 9999;
        // 发送的内容
        String message = "Hello,UDP!";
        byte[] bytes = message.getBytes();
        // 发送内容的长度
        int len = bytes.length;
        // 发送的包裹
        DatagramPacket packet = new DatagramPacket(bytes, len, inetAddress, port);
        // 发送
        socket.send(packet);
    }
}
